package com.miaoxun.dao.provider;

import com.miaoxun.dao.util.ClassUtil;
import com.miaoxun.domain.enums.ErrEnum;
import com.miaoxun.domain.exception.ThException;
import org.apache.ibatis.jdbc.SQL;
import org.apache.poi.ss.formula.functions.T;

import java.util.Map;

/**
 * 自定义provider
 */
public class BaseProvider {

    public String getAll(String clazzName) {
        SQL sql = new SQL();
        sql.FROM(ClassUtil.class2table(clazzName));
        sql.SELECT(ClassUtil.field2comun(clazzName));
        return sql.toString();
    }


    public String getModelById(Object key, String clazzName) {
        SQL sql = new SQL();
        sql.FROM(ClassUtil.class2table(clazzName));
        sql.SELECT(ClassUtil.field2comun(clazzName));
        sql.WHERE("1=1");
        sql.AND().WHERE(ClassUtil.GetPrimaryKey(clazzName) + "=" + key);
        return sql.toString();

    }


    public String update(Object o) {
        SQL sql = new SQL();

        sql.UPDATE(ClassUtil.class2table(o.getClass()));
        Map<String, String> map = ClassUtil.class2update(o, true);

        for (Map.Entry<String, String> entry : map.entrySet()) {
            sql.SET(entry.getKey() + "=" + entry.getValue());
        }

        sql.WHERE("1=1").AND().WHERE(ClassUtil.GetPrimaryKey(o.getClass()) + "=" + ClassUtil.GetPrimaryValue(o));

        return sql.toString();

    }

    public String deleteById(Object key, String clazzName) {
        SQL sql = new SQL();

        sql.DELETE_FROM(ClassUtil.class2table(clazzName));

        sql.WHERE("1=1").AND().WHERE(ClassUtil.GetPrimaryKey(clazzName) + "=" + key);

        return sql.toString();
    }


    public String delete(Object entity) {
        SQL sql = new SQL();
        sql.DELETE_FROM(ClassUtil.class2table(entity.getClass()));
        sql.WHERE("1=1");
        sql.AND().WHERE(ClassUtil.GetPrimaryKey(entity.getClass()) + "=" + ClassUtil.GetPrimaryValue(entity));
        return sql.toString();
    }

    public String insert(Object o) {
        SQL sql = new SQL();

        sql.INSERT_INTO(ClassUtil.class2table(o.getClass()));
        Map<String, String> map = ClassUtil.class2update(o, false);

        for (Map.Entry<String, String> entry : map.entrySet()) {
            sql.INTO_COLUMNS(entry.getKey());
            sql.INTO_VALUES(entry.getValue());
        }

        return sql.toString();
    }


    /**
     * modellist
     *
     * @return
     */
    public String getModelList(Map<String, String> map, Object clazzName) {
        SQL sql = new SQL();

        sql.FROM(ClassUtil.class2table(String.valueOf(clazzName)));
        sql.SELECT(ClassUtil.field2comun(String.valueOf(clazzName)));
        sql.WHERE("1=1");
        for (Map.Entry<String, String> entry : map.entrySet()) {

            sql.AND().WHERE(ClassUtil.keyValue2colmun(entry.getKey(), entry.getValue()));

        }
        return sql.toString();
    }


    /**
     * max
     *
     * @param map
     * @param clazzName
     * @return
     */
    public String max(Map<String, String> map, Object clazzName, String filed) {
        SQL sql = new SQL();

        sql.FROM(ClassUtil.class2table(String.valueOf(clazzName)));
        sql.SELECT(filed);
        sql.WHERE("1=1");
        for (Map.Entry<String, String> entry : map.entrySet()) {

            sql.AND().WHERE(ClassUtil.keyValue2colmun(entry.getKey(), entry.getValue()));

        }
        return sql.toString();
    }

    /**
     * 主键批量删除
     *
     * @param ids
     * @param clazzName
     * @return
     */
    public String batchRemove(Object[] ids, String clazzName) throws ThException {
        if (ids == null || ids.length < 1)
            throw new ThException(ErrEnum.DELETE);
        SQL sql = new SQL();
        sql.DELETE_FROM(ClassUtil.class2table(clazzName));
        sql.WHERE("1=1");

        String key = ClassUtil.GetPrimaryKey(clazzName);


        StringBuilder del = new StringBuilder(key + " in ( ");

        for (Object id : ids) {
            if (id instanceof String)
                del.append("'" + id + "'").append(",");
            else
                del.append(id).append(",");
        }
        sql.AND().WHERE(del.substring(0, del.length() - 1) + ")");


        return sql.toString();
    }


}
